package stephencarmody.fonttexture;

import java.awt.Font;
import java.awt.GraphicsEnvironment;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JToolBar;

public final class ToolBar extends JToolBar implements ActionListener {
	
    private static final long serialVersionUID = 0;

    // GUI Components
    private JButton saveBtn;
    private JComboBox familyCombo;
    private JComboBox styleCombo;
    private ImageSaveDialog saveDlg;
    
    public ToolBar() {
    	setFloatable(false);
    	
    	// Add Save Button to ToolBar
    	saveBtn = new JButton();
    	saveBtn.setIcon(new ImageIcon(this.getClass().getResource("icons/save.png")));
    	saveBtn.addActionListener(this);
    	add(saveBtn);

    	// Add Font Family ComboBox to ToolBar
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        String families[] = ge.getAvailableFontFamilyNames();
        familyCombo = new JComboBox(families);
        familyCombo.addActionListener(this);
        familyCombo.setPreferredSize(familyCombo.getMinimumSize());
        familyCombo.setMaximumSize(familyCombo.getMinimumSize());
        add(familyCombo);
        
        // Add Font Style ComboBox to ToolBar
        String styles[] = {"plain","italic","bold","bold italic"};
        styleCombo = new JComboBox(styles);
        styleCombo.addActionListener(this);
        styleCombo.setPreferredSize(styleCombo.getMinimumSize());
        styleCombo.setMaximumSize(styleCombo.getMinimumSize());
        add(styleCombo);
        
        saveDlg = new ImageSaveDialog();
    }
    
    /** Receives ActionEvents from ToolBar components */
    public void actionPerformed(ActionEvent event) {
    	// Save
    	if ( event != null && saveBtn == event.getSource() ) {
    		if ( JFileChooser.APPROVE_OPTION == saveDlg.showSaveDialog(this) ) {
    			// Ensure selected file has correct extension for the selected filter
    			// before saving texture to it
    			File f = saveDlg.getSelectedFile();
    			String e = ((ImageFilter)saveDlg.getFileFilter()).getExtension();
    			String ext = ImageFilter.getExtension(f);
    			try {
    				if ( ext == null || ! ext.equals(e) ) {
    					f = new File(f.getCanonicalPath() + "." + e);
    				}
    				FontTexture.save(f);
				} catch( IOException exp ) {}
    		}
    		return;
    	}
    	
    	// Font family or style change
        String family = familyCombo.getSelectedItem().toString();
        int style = Font.PLAIN;
        String selected = styleCombo.getSelectedItem().toString();
        if ( selected.equals("italic") ) {
            style = Font.ITALIC;
        } else if ( selected.equals("bold") ) {
            style = Font.BOLD;
        } else if ( selected.equals("bold italic") ) {
            style = Font.BOLD + Font.ITALIC;
        }
    	FontTexture.update(family, style);
    }
    
}
